package com.atguigu.yygh.order.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable String scheduleId,
                         @PathVariable Long patientId){
        Long orderId = orderService.saveOrder(scheduleId,patientId);
        return R.ok().data("orderId",orderId);
    }

        //带条件分页查询订单列表
    @ApiOperation(value = "订单列表（条件查询带分页）")
    @GetMapping("auth/{page}/{limit}")
    public R list(@PathVariable Long page,
                  @PathVariable Long limit,
                  OrderQueryVo orderQueryVo, HttpServletRequest request){

        Long userId = AuthContextHolder.getUserId(request);
        orderQueryVo.setUserId(userId);
       Page<OrderInfo> pageParam=new Page<>(page,limit);
       IPage<OrderInfo> pageModel=orderService.selectPage(pageParam,orderQueryVo);
        return  R.ok().data("pageModel",pageModel);

    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public R getStatusList() {
        return R.ok().data("statusList", OrderStatusEnum.getStatusList());
    }


    //根据订单id查询订单详情
    @ApiOperation(value = "根据订单id查询订单详情")
    @GetMapping("auth/getOrders/{orderId}")
    public R getOrders(@PathVariable Long orderId ){
       OrderInfo orderInfo= orderService.getOrderInfo(orderId);
       return R.ok().data("orderInfo",orderInfo);
    }


    //取消预约
    @ApiOperation("取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public R cancelOrder(
            @PathVariable("orderId") Long orderId){

       boolean flag= orderService.cancelOrder(orderId);
       return  R.ok().data("flag",flag);

    }

    //获取订单统计数据
    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo){

        Map<String, Object> map=orderService.getCountMap(orderCountQueryVo);
        return map;

    }




}
